package othello.players.benchmarkplayers;


import java.awt.Color;
import java.awt.Point;
import java.util.ArrayList;

import othello.model.IBoard;
import othello.players.AbstractPlayer;

/**
 * Plays completely randomly.
 * Finds a list of all valid moves, then randomly picks one.
 */
public class Random extends AbstractPlayer {

    private java.util.Random _rand;

    public Random() {
        super("Spacey");
    }

    @Override
    public void joinBoard(IBoard board, Color color) {
        super.joinBoard(board, color);
        setRand(new java.util.Random());
    }

    @Override
    public void beginTurn() {
        ArrayList<Point> moves = validMoves();

        if (moves.isEmpty()) {
            getBoard().pass(this);
        } else {
            Point pt = moves.get(getRand().nextInt(moves.size()));
            if (!board.placePiece(this, pt.y, pt.x)) {
                getBoard().pass(this);
            }
        }
    }

    @Override
    public void endTurn() {
    }
    
    @Override
	public void endGame() {
	}

    /**
     * Finds all the valid moves for the given player on the given board.
     *
     * @param board
     * @param playerColor
     * @return
     */
    protected ArrayList<Point> validMoves(IBoard board, Color playerColor) {
        ArrayList<Point> moves = new ArrayList<Point>();

        // Find all valid moves.
        for (int r = 0; r < board.getRowCount(); r++) {
            for (int c = 0; c < board.getColCount(); c++) {
                if (board.isValidMove(playerColor, r, c)) {
                    // Make the first valid move we can find.
                    moves.add(new Point(c, r));
                }
            }
        }

        return moves;
    }

    /**
     * Finds all the valid moves for this player on its current board.
     *
     * @return
     */
    protected ArrayList<Point> validMoves() {
        return validMoves(getBoard(), getColor());
    }

    /**
     * @return the board
     */
    public IBoard getBoard() {
        return board;
    }

    /**
     * @param board the board to set
     */
    public void setBoard(IBoard board) {
        this.board = board;
    }

    /**
     * @return the _rand
     */
    public java.util.Random getRand() {
        return _rand;
    }

    /**
     * @param rand the _rand to set
     */
    public void setRand(java.util.Random rand) {
        this._rand = rand;
    }
}
